home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell⁄THINK C / Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  6.3 KB  |  339 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     CShell
  5. ** File:        utils.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  19. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  21.  
  22. #ifndef __STRING__
  23. #include <String.h>
  24. #endif
  25.  
  26. #ifndef __RESOURCES__
  27. #include <Resources.h>
  28. #endif
  29.  
  30. #ifndef __UTILITIES__
  31. #include "Utilities.h"
  32. #endif
  33.  
  34.  
  35.  
  36. #define BASE 10
  37.  
  38.  
  39.  
  40. /*****************************************************************************/
  41.  
  42.  
  43.  
  44. #pragma segment Main
  45. void    appendi2cstr(char *cstr, short i)
  46. {
  47.     i2cstr(cstr + strlen(cstr), i);
  48. }
  49.  
  50.  
  51.  
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. #pragma segment Main
  57. short    appendi2pstr(char *pstr, short i)
  58. {
  59.     short    j;
  60.  
  61.     j = 0;
  62.     if (i >= BASE) j = appendi2pstr(pstr, i / BASE);
  63.     pstr[++*pstr] = "0123456789ABCDEF"[i - j];
  64.     return(BASE * i);
  65. }
  66.  
  67.  
  68.  
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. #pragma segment Main
  74. short    i2cstr(char *cstr, short i)
  75. {
  76.     short    j;
  77.  
  78.     cstr[1] = j = 0;
  79.     if (i >= BASE)
  80.         j = i2cstr(cstr + 1, i / BASE);
  81.     *cstr = "0123456789ABCDEF"[i - j];
  82.     return(BASE * i);
  83. }
  84.  
  85.  
  86.  
  87. /*****************************************************************************/
  88.  
  89.  
  90.  
  91. #pragma segment Main
  92. void    i2pstr(char *pstr, short i)
  93. {
  94.     *pstr = 0;
  95.     appendi2pstr(pstr, i);
  96. }
  97.  
  98.  
  99.  
  100. /*****************************************************************************/
  101.  
  102.  
  103.  
  104. #pragma segment Main
  105. void    pstrcat(char *d, char *s)
  106. {
  107.     short    i;
  108.  
  109.     for (i = 0; i < s[0];)
  110.         d[++d[0]] = s[++i];
  111. }
  112.  
  113.  
  114.  
  115. /*****************************************************************************/
  116.  
  117.  
  118.  
  119. #pragma segment Main
  120. void    pstrcpy(char *d, char *s)
  121. {
  122.     short    i;
  123.  
  124.     i = *s;
  125.     do {
  126.         d[i] = s[i];
  127.     } while (i--);
  128. }
  129.  
  130.  
  131.  
  132. /*****************************************************************************/
  133.  
  134.  
  135.  
  136. #pragma segment Main
  137. short    GetHexByte(char *cptr)
  138. {
  139.     short    val, i, chr;
  140.  
  141.     for (val = 0, i = 0; i < 2; ++i) {
  142.         chr = cptr[i];
  143.         if (chr == '=') return(cptr[++i]);
  144.         if (chr > 'F') chr -= 0x20;
  145.         if (chr > '9') chr -= ('A' - '9' - 1);
  146.         val = (val << 4) + chr - '0';
  147.     }
  148.     return(val);
  149. }
  150.  
  151.  
  152.  
  153. /*****************************************************************************/
  154.  
  155.  
  156.  
  157. /* This code expects the key equivalents to be in item #2, which is a StatText
  158. ** item that is located so the text is outside of the dialog.  This allows us
  159. ** to put key equivalent information in the resource fork, so the key
  160. ** equivalents are localizable. */
  161.  
  162. #pragma segment Main
  163. pascal Boolean    keyEquivFilter(DialogPtr dlg, EventRecord *event, short *item)
  164. {
  165.     short    itemType;
  166.     Handle    itemHndl;
  167.     Rect    itemRect;
  168.     Str255    itemText;
  169.     short    i, theChr, theMod, equivChr, modMask, modVal, itemNum;
  170.     long    tick;
  171.  
  172.     if (event->what == updateEvt) {
  173.         if (dlg == (DialogPtr)event->message) OutlineDialogItem(dlg, 1);
  174.         return(false);
  175.     }
  176.  
  177.     if (event->what != keyDown) return(false);
  178.  
  179.     itemNum = 0;
  180.  
  181.     theChr = event->message   & charCodeMask;
  182.     theMod = event->modifiers & keyCodeMask;
  183.  
  184.     if ((theChr == 0x0D) || (theChr == 0x03)) {        /* If return or enter... */
  185.         if (!(theMod & (cmdKey + optionKey + controlKey))) itemNum = 1;
  186.     }        
  187.     else {
  188.  
  189.         GetDItem(dlg, 2, &itemType, &itemHndl, &itemRect);
  190.         GetIText(itemHndl, itemText);
  191.  
  192.         for (i = 1; i <= *itemText; i += 9) {
  193.             equivChr = GetHexByte((char *) itemText + i);
  194.             modMask  = GetHexByte((char *) itemText + i + 2) << 8;
  195.             modVal   = GetHexByte((char *) itemText + i + 4) << 8;
  196.             itemNum  = GetHexByte((char *) itemText + i + 6);
  197.             if (theChr == equivChr)
  198.                 if ((theMod & modMask) == modVal) break;
  199.             itemNum = 0;
  200.         }
  201.     }
  202.  
  203.     if (itemNum) {
  204.         GetDItem(dlg, itemNum, &itemType, &itemHndl, &itemRect);
  205.         HiliteControl((ControlHandle)itemHndl, 1);
  206.         tick = TickCount();
  207.         while (TickCount() < tick + 10);
  208.         HiliteControl((ControlHandle)itemHndl, 0);
  209.         *item = itemNum;
  210.         return(true);
  211.     }
  212.  
  213.     return(false);
  214. }
  215.  
  216.  
  217.  
  218. /*****************************************************************************/
  219.  
  220.  
  221.  
  222. #pragma segment Main
  223. void    OffsetControl(ControlHandle ctl, short dx, short dy)
  224. {
  225.     Rect    ctlRect;
  226.  
  227.     ctlRect = (*ctl)->contrlRect;
  228.     MoveControl(ctl, ctlRect.left + dx, ctlRect.top + dy);
  229. }
  230.  
  231.  
  232.  
  233. /*****************************************************************************/
  234.  
  235.  
  236.  
  237. #pragma segment Main
  238. void    DoDrawGrowIcon(WindowPtr window, Boolean horLine, Boolean verLine)
  239. {
  240.     WindowPtr    oldPort;
  241.     Rect        rct;
  242.     RgnHandle    oldClip, newClip;
  243.  
  244.     GetPort(&oldPort);
  245.     SetPort(window);
  246.  
  247.     rct = window->portRect;
  248.     rct.left = rct.right  - 15;
  249.     rct.top  = rct.bottom - 15;
  250.  
  251.     if (window != FrontWindow()) {
  252.         FrameRect(&rct);
  253.         ++rct.top;
  254.         ++rct.left;
  255.         EraseRect(&rct);
  256.         SetPort(oldPort);
  257.         return;
  258.     }
  259.  
  260.     oldClip = NewRgn();
  261.     newClip = NewRgn();
  262.  
  263.     if (horLine) rct.left = window->portRect.left;
  264.     if (verLine) rct.top  = window->portRect.top;
  265.     RectRgn(newClip, &rct);
  266.  
  267.     GetClip(oldClip);
  268.     SetClip(newClip);
  269.     DrawGrowIcon(window);        /* Draw grow icon without scrollbar lines. */
  270.  
  271.     SetClip(oldClip);
  272.     DisposeRgn(oldClip);
  273.     DisposeRgn(newClip);
  274.  
  275.     SetPort(oldPort);
  276. }
  277.  
  278.  
  279.  
  280. /*****************************************************************************/
  281.  
  282.  
  283.  
  284. #pragma segment Main
  285. void    DoDrawControls(WindowPtr window, Boolean scrollBarsOnly)
  286. {
  287.     ControlHandle    ctl;
  288.  
  289.     ctl = ((WindowPeek)window)->controlList;
  290.     while (ctl) {
  291.         DoDraw1Control(ctl, scrollBarsOnly);
  292.         ctl = (*ctl)->nextControl;
  293.     }
  294. }
  295.  
  296.  
  297.  
  298. /*****************************************************************************/
  299.  
  300.  
  301.  
  302. #pragma segment Main
  303. void    DoDraw1Control(ControlHandle ctl, Boolean scrollBarsOnly)
  304. {
  305.     Boolean            front;
  306.     WindowPtr        window, oldPort;
  307.     Rect            rct;
  308.     ControlHandle    scrollCtl;
  309.     static Handle    scrollProc;
  310.  
  311.     front = ((window = (*ctl)->contrlOwner) == FrontWindow());
  312.  
  313.     if (!scrollProc) {
  314.         SetRect(&rct, 0, 0, 16, 100);
  315.         scrollCtl = NewControl(window, &rct, nil, false, 0, 0, 0, scrollBarProc, 0);
  316.         if (scrollCtl) {
  317.             scrollProc = (*scrollCtl)->contrlDefProc;
  318.             DisposeControl(scrollCtl);
  319.         }
  320.     }
  321.  
  322.     if ((*ctl)->contrlDefProc == scrollProc) {
  323.         if (front) Draw1Control(ctl);
  324.         else {
  325.             GetPort(&oldPort);
  326.             SetPort(window);
  327.             rct = (*ctl)->contrlRect;
  328.             FrameRect(&rct);
  329.             InsetRect(&rct, 1, 1);
  330.             EraseRect(&rct);
  331.             SetPort(oldPort);
  332.         }
  333.     }
  334.     else if (!scrollBarsOnly) Draw1Control(ctl);
  335. }
  336.  
  337.  
  338.  
  339.